/**
 * @(#) InrecMediator.as
 *
 * @date		2013. 05. 30.
 * @version		1.0
 * @author		Park, Jutae
 * @description	콘도추천 신청 - Mediator
 * 				Inrec를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.nametag.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.nametag.NametagFacade;
	import com.skt.common_hr.nametag.model.NametagProxy;
	import com.skt.common_hr.nametag.model.vo.NametagVO;
	import com.skt.common_hr.nametag.model.vo.reqNametagVO;
	import com.skt.common_hr.nametag.view.components.NametagView;
	import com.skt.common_hr.nametag.view.components.ReqNametag;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class NametagMediator extends OverViewMediator
	{
		public static const NAME:String = "InrecMediator"; 

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function NametagMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
			super.initializeView();
 		}
		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			view.setDateRange();
 			loadList();
 		} 
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		public function mainList():void
 		{
 			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.WriteViewLoad(Variables.myPernr);	
 		}
 		
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(NametagView.sinChung, onWriteViewLoad);
 			view.addEventListener(ReqNametag.OkSave, onSave);
 			view.addEventListener(NametagView.dgClick, onRead);
 			view.addEventListener(NametagView.Update, onUpdate);
			view.addEventListener(NametagView.Delete, onDelete); 
			view.addEventListener(NametagView.Serach, loadList); 			
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(NametagView.sinChung, onWriteViewLoad);
 			view.removeEventListener(ReqNametag.OkSave, onSave);
 			view.removeEventListener(NametagView.dgClick, onRead);
 			view.removeEventListener(NametagView.Update, onUpdate);
 			view.removeEventListener(NametagView.Delete, onDelete);
 		}
 		
 		/* 명함화면 클릭시 List 조회 */
 		private function loadList(evt:Event = null):void 
 		{
 			view.vs.selectedIndex = 0;
 			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.load(Variables.myPernr, view.sDay, view.eDay);
 		}
 		
 		/* 명함신청 버튼 클릭시 */
 		private function onWriteViewLoad(e:Event):void
 		{
 			mainList();
 			
 			view.reqView.rbLeft.selected	= true;
 			view.reqView.rbCenter.selected 	= false;
 			view.reqView.rbRight.selected 	= false;
 			
 			view.reqView.rbCenter.enabled	= true;
 			view.reqView.rbLeft.enabled		= true;
 			view.reqView.rbRight.enabled	= true;
 			view.reqView.cb.enabled 		= true;
 			
 			view.vs.selectedIndex = 1;
 			
 			view.reqView.cb.selectedIndex = 0;
 			
 			view.reqView.rbLeft.selected = true;
 			view.reqView.rbCenter.selected = false;
 			view.reqView.rbRight.selected = false;
 			view.reqView.lbJWTXT.visible	= true;
			view.reqView.lbJWTXT_EN.visible = true;
			view.reqView.lbJCTXT.visible	= false;
			view.reqView.lbJCTXT_EN.visible = false;
			view.reqView.lbHEAD.visible	    = false;
			view.reqView.lbHEAD_EN.visible  = false;
			
			view.reqView.btnSave.visible	= true;
 		}
 		
		/* 명함신청 데이타그리드 클릭시 */
		private function onRead(event:Event):void
		{
			var dMonth:String;
				
			var _vo:NametagVO = new NametagVO();
 				_vo =  view.dg.selectedItem as NametagVO;
			
			dMonth	= _vo.RQMON;
			
			view.reqView.lbSDATE.text   = _vo.SDATE;
			view.reqView.lbWSTATX.text	= _vo.WSTATX;
			view.reqView.lbRTEXT.text	= _vo.RTEXT;
			
			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.readLoad(Variables.myPernr, dMonth);
		}
		
		/* 변경버튼 클릭시 */
		private function onUpdate(event:Event):void
		{
			var dMonth:String;
			var _vo:NametagVO = new NametagVO();
 				_vo =  view.dg.selectedItem as NametagVO;
 			
 			view.reqView.lbSDATE.text   = _vo.SDATE;
			view.reqView.lbWSTATX.text	= _vo.WSTATX;
			view.reqView.lbRTEXT.text	= _vo.RTEXT;
 				
			dMonth	= _vo.RQMON;
			
			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.update(Variables.myPernr, dMonth);
		}
		
		/* 저장버튼 클릭시 */
		public function onSave(e:Event):void
		{
			var vo:reqNametagVO = new reqNametagVO();
			 
			vo.EMAIL = view.reqView.lbEMAIL.text;		 
 			vo.ENAME = view.reqView.lbENAME.text;		
 			vo.ENAME_EN = view.reqView.lbENAME_EN.text;	
 			vo.MOVETEL = view.reqView.lbMOVETEL.text;	
 			vo.OFFADR = view.reqView.lbOFFADR.text;		
 			vo.OFFADR_EN = view.reqView.lbOFFADR_EN.text;
 			vo.ORGTX = view.reqView.lbORGTX.text;		
 			vo.ORGTX_EN = view.reqView.lbORGTX_EN.text;	
 			
 			vo.JWTXT = view.reqView.lbJWTXT.text;		
 			vo.JWTXT_EN = view.reqView.lbJWTXT_EN.text;	
 			vo.JCTXT = view.reqView.lbJCTXT.text;		
 			vo.JCTXT_EN = view.reqView.lbJCTXT_EN.text;
 			vo.HEAD = view.reqView.lbHEAD.text;		
 			vo.HEAD_EN = view.reqView.lbHEAD_EN.text;		
 			
 			vo.RTEXT = view.reqView.lbRTEXT.text;
 			vo.SDATE = view.reqView.lbSDATE.text;		
 			vo.WSTATX = view.reqView.lbWSTATX.text;		
 			
 			vo.SREASN = view.reqView.cb.selectedIndex;
 			if(view.reqView.rbLeft.selected == true){
 				vo.SCHK = '1';	
 			}else if(view.reqView.rbCenter.selected == true){
 				vo.SCHK = '2';
 			}else {
 				vo.SCHK = '3';
 			}
 			
 			if(view.sType == 'S'){
 				var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 				proxy.save(Variables.myPernr, vo);	
 			}else {
 				var dMonth:String;
				var _vo:NametagVO = new NametagVO();
	 				_vo =  view.dg.selectedItem as NametagVO;
	 				
	 			dMonth = _vo.RQMON;
 				
 				var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 				proxy.save(Variables.myPernr, vo, dMonth);
 			}
		}
		
		/* 명함신청 삭제 클릭시 */
		private function onDelete(event:Event):void
		{
			var dMonth:String;
			var _vo:NametagVO = new NametagVO();
 				_vo =  view.dg.selectedItem as NametagVO;
 				
 			dMonth = _vo.RQMON;	
			
 			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.del(Variables.myPernr, dMonth); 
		}
 		
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					NametagFacade.LOAD_LIST_COMPLETE,
					NametagFacade.LOAD_LIST_FAILED,
					NametagFacade.WRITEVIEW_LIST_COMPLETE,
					NametagFacade.WRITEVIEW_LIST_FAILED,
					NametagFacade.LOAD_CBLIST_COMPLETE,
					NametagFacade.LOAD_CBLIST_FAILED,
					NametagFacade.SAVE_COMPLETE,
					NametagFacade.SAVE_FAILED,
					NametagFacade.DELETE_COMPLETE,
					NametagFacade.DELETE_FAILED,
					NametagFacade.READ_LIST_COMPLETE,
					NametagFacade.READ_LIST_FAILED,
					NametagFacade.UPDATE_COMPLETE,
					NametagFacade.UPDATE_FAILED,
					
					NametagFacade.CANCEL_REQUEST_INPUT
					
					]);
		}
		
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case NametagFacade.LOAD_LIST_COMPLETE:
					onLoadList(note.getBody() as NametagProxy);
					break;
				case NametagFacade.LOAD_LIST_FAILED:
					break;
				case NametagFacade.WRITEVIEW_LIST_COMPLETE:
					onWriteList(note.getBody() as NametagProxy);
					break;
				case NametagFacade.WRITEVIEW_LIST_FAILED:
					break;	
				case NametagFacade.LOAD_CBLIST_COMPLETE:
					onCbList(note.getBody() as NametagProxy);
					break;
				case NametagFacade.LOAD_CBLIST_FAILED:
					break;
				case NametagFacade.SAVE_COMPLETE:
					Alert.show('저장되었습니다.');
					loadList();
					break;
				case NametagFacade.SAVE_FAILED:
					onfailAlert(note.getBody() as NametagProxy);
					break;	
				case NametagFacade.READ_LIST_COMPLETE:
					view.vs.selectedIndex = 1;
					onReadList(note.getBody() as NametagProxy);
					break;
				case NametagFacade.READ_LIST_FAILED:
					break;
				case NametagFacade.UPDATE_COMPLETE:
					view.vs.selectedIndex = 1;
					onUpdateList(note.getBody() as NametagProxy);
					break;
				case NametagFacade.UPDATE_FAILED:
					break;	
				case NametagFacade.DELETE_COMPLETE:
					loadList();
					break;
				case NametagFacade.DELETE_FAILED:
					break;		
								
				case NametagFacade.OK_REQUEST_INPUT:
					view.vs.selectedIndex = 0;
					loadList();
					break;	
				default:
					break;
			}
		}

 		private function onLoadList(proxy:NametagProxy):void
 		{
 			view.button1.visible = proxy.sVisible;
 			view.dg.dataProvider = proxy.NametagList;
 			cbListload();
 		}
 		
 		private function onWriteList(proxy:NametagProxy):void
 		{
 			view.reqView.rbLeft.selected	= true;
 			view.reqView.rbCenter.selected 	= false;
 			view.reqView.rbRight.selected 	= false;
 			
 			view.reqView.rbCenter.enabled	= true;
 			view.reqView.rbLeft.enabled		= true;
 			view.reqView.rbRight.enabled	= true;
 			view.reqView.cb.enabled 		= true;
 			
 			var vo:reqNametagVO = proxy.reqNametagList.getItemAt(0) as reqNametagVO;
 			
 			view.reqView.lbEMAIL.text		= vo.EMAIL;
 			view.reqView.lbENAME.text		= vo.ENAME;
 			view.reqView.lbENAME_EN.text	= vo.ENAME_EN;
 			view.reqView.lbMOVETEL.text		= vo.MOVETEL;
 			view.reqView.lbOFFADR.text		= vo.OFFADR;
 			view.reqView.lbOFFADR_EN.text	= vo.OFFADR_EN;
 			view.reqView.lbORGTX.text		= vo.ORGTX;
 			view.reqView.lbORGTX_EN.text	= vo.ORGTX_EN;
 			
 			view.reqView.lbJWTXT.text		= vo.JWTXT;
 			view.reqView.lbJWTXT_EN.text	= vo.JWTXT_EN;
 			view.reqView.lbJCTXT.text		= vo.JCTXT;
 			view.reqView.lbJCTXT_EN.text	= vo.JCTXT_EN;
 			view.reqView.lbHEAD.text		= vo.HEAD;
 			view.reqView.lbHEAD_EN.text		= vo.HEAD_EN;
 			
 			view.reqView.lbRTEXT.text		= vo.RTEXT;
 			view.reqView.lbSDATE.text		= vo.SDATE;
 			view.reqView.lbWSTATX.text		= vo.WSTATX;
 			
 			if(vo.SCHK == ''){
 				view.reqView.lbJWTXT.visible	= true;
				view.reqView.lbJWTXT_EN.visible = true;
				view.reqView.lbJCTXT.visible	= false;
				view.reqView.lbJCTXT_EN.visible = false;
				view.reqView.lbHEAD.visible	    = false;
				view.reqView.lbHEAD_EN.visible  = false;
 			}
 			
 			view.reqView.lbEnglishname.visible = true;
 			view.reqView.lbEnglishname.includeInLayout = true;
 		}
 		
 		public function onCbList(proxy:NametagProxy):void
 		{
 			view.reqView.cb.dataProvider = proxy.cbList;
 		}
 		
 		public function cbListload():void
 		{
 			var proxy:NametagProxy = facade.retrieveProxy(NametagProxy.NAME) as NametagProxy;
 			proxy.comboListload(Variables.myPernr);
 		}
 		
 		public function onfailAlert(proxy:NametagProxy):void
 		{
 			Alert.show(proxy.failedAlert);
 		}
 		
 		private function onReadList(proxy:NametagProxy):void
 		{
 			view.reqView.rbCenter.enabled	= false;
 			view.reqView.rbLeft.enabled		= false;
 			view.reqView.rbRight.enabled	= false;
 			view.reqView.cb.enabled 		= false;
 			
 			view.reqView.btnSave.visible	= false;
 			
			var vo:reqNametagVO = proxy.readList.getItemAt(0) as reqNametagVO;
 			
			view.reqView.cb.selectedIndex	= vo.SREASN;
			
			view.reqView.lbENAME.text		= vo.ENAME;
			view.reqView.lbENAME_EN.text  	= vo.ENAME_EN;
			view.reqView.lbORGTX.text		= vo.ORGTX;
			view.reqView.lbORGTX_EN.text	= vo.ORGTX_EN;
			view.reqView.lbMOVETEL.text		= vo.MOVETEL;
			view.reqView.lbEMAIL.text		= vo.EMAIL;
			view.reqView.lbOFFADR.text		= vo.OFFADR;
			view.reqView.lbOFFADR_EN.text	= vo.OFFADR_EN;

			view.reqView.lbJWTXT.text		= vo.JWTXT;
			view.reqView.lbJWTXT_EN.text	= vo.JWTXT_EN;
			view.reqView.lbJCTXT.text		= vo.JCTXT;
			view.reqView.lbJCTXT_EN.text	= vo.JCTXT_EN;
			view.reqView.lbHEAD.text		= vo.HEAD;
			view.reqView.lbHEAD_EN.text		= vo.HEAD_EN;
			
			if(vo.SCHK == '1'){
 				view.reqView.rbLeft.selected = true;
 				view.reqView.rbCenter.selected = false;
 				view.reqView.rbRight.selected = false;
 				view.reqView.lbJWTXT.visible	= true;
				view.reqView.lbJWTXT_EN.visible = true;
				view.reqView.lbJCTXT.visible	= false;
				view.reqView.lbJCTXT_EN.visible = false;
				view.reqView.lbHEAD.visible	    = false;
				view.reqView.lbHEAD_EN.visible  = false;
 			}else if(vo.SCHK == '2'){
 				view.reqView.rbLeft.selected = false;
 				view.reqView.rbCenter.selected = true;
 				view.reqView.rbRight.selected = false;
 				view.reqView.lbJWTXT.visible	= false;
				view.reqView.lbJWTXT_EN.visible = false;
				view.reqView.lbJCTXT.visible	= true;
				view.reqView.lbJCTXT_EN.visible = true;
				view.reqView.lbHEAD.visible	    = false;
				view.reqView.lbHEAD_EN.visible  = false;
 			}else {
 				view.reqView.rbLeft.selected = false;
 				view.reqView.rbCenter.selected = false;
 				view.reqView.rbRight.selected = true;
 				view.reqView.lbJWTXT.visible	= false;
				view.reqView.lbJWTXT_EN.visible = false;
				view.reqView.lbJCTXT.visible	= false;
				view.reqView.lbJCTXT_EN.visible = false;
				view.reqView.lbHEAD.visible	    = true;
				view.reqView.lbHEAD_EN.visible  = true;
 			}
 			
 			view.reqView.lbEnglishname.visible = false;
 			view.reqView.lbEnglishname.includeInLayout = false;
 		}
 		
 		private function onUpdateList(proxy:NametagProxy):void
 		{
 			view.reqView.rbCenter.enabled = true;
			view.reqView.rbLeft.enabled   = true;
			view.reqView.rbRight.enabled  = true;
			view.reqView.cb.enabled		  = true;
			
			view.reqView.btnSave.visible	= true;
 			
			var vo:reqNametagVO = proxy.updateList.getItemAt(0) as reqNametagVO;

			view.reqView.cb.selectedIndex	= vo.SREASN;
			
			view.reqView.lbENAME.text		= vo.ENAME;
			view.reqView.lbENAME_EN.text  	= vo.ENAME_EN;
			view.reqView.lbORGTX.text		= vo.ORGTX;
			view.reqView.lbORGTX_EN.text	= vo.ORGTX_EN;
			view.reqView.lbMOVETEL.text		= vo.MOVETEL;
			view.reqView.lbEMAIL.text		= vo.EMAIL;
			view.reqView.lbOFFADR.text		= vo.OFFADR;
			view.reqView.lbOFFADR_EN.text	= vo.OFFADR_EN;
			
			view.reqView.lbJWTXT.text		= vo.JWTXT;
			view.reqView.lbJWTXT_EN.text	= vo.JWTXT_EN;
			view.reqView.lbJCTXT.text		= vo.JCTXT;
			view.reqView.lbJCTXT_EN.text	= vo.JCTXT_EN;
			view.reqView.lbHEAD.text		= vo.HEAD;
			view.reqView.lbHEAD_EN.text		= vo.HEAD_EN;
 			
 			if(vo.SCHK == '1'){
 				view.reqView.rbLeft.selected = true;
 				view.reqView.rbCenter.selected = false;
 				view.reqView.rbRight.selected = false;
 				view.reqView.lbJWTXT.visible	= true;
				view.reqView.lbJWTXT_EN.visible = true;
				view.reqView.lbJCTXT.visible	= false;
				view.reqView.lbJCTXT_EN.visible = false;
				view.reqView.lbHEAD.visible	    = false;
				view.reqView.lbHEAD_EN.visible  = false;
 			}else if(vo.SCHK == '2'){
 				view.reqView.rbLeft.selected = false;
 				view.reqView.rbCenter.selected = true;
 				view.reqView.rbRight.selected = false;
 				view.reqView.lbJWTXT.visible	= false;
				view.reqView.lbJWTXT_EN.visible = false;
				view.reqView.lbJCTXT.visible	= true;
				view.reqView.lbJCTXT_EN.visible = true;
				view.reqView.lbHEAD.visible	    = false;
				view.reqView.lbHEAD_EN.visible  = false;
 			}else {
 				view.reqView.rbLeft.selected = false;
 				view.reqView.rbCenter.selected = false;
 				view.reqView.rbRight.selected = true;
 				view.reqView.lbJWTXT.visible	= false;
				view.reqView.lbJWTXT_EN.visible = false;
				view.reqView.lbJCTXT.visible	= false;
				view.reqView.lbJCTXT_EN.visible = false;
				view.reqView.lbHEAD.visible	    = true;
				view.reqView.lbHEAD_EN.visible  = true;
 			}
 			
 			view.reqView.lbEnglishname.visible = true;
 			view.reqView.lbEnglishname.includeInLayout = true;
 		}
 		
 		public function addReadTextInput(ac:ArrayCollection):void
 		{
				
 		}
 		
        private function get view():NametagView
        {
            return viewComponent as NametagView;
        }
		
	}
}